//
//  ST_PairsCenterHeaderView.swift
//  btvideo
//
//  Created by mac on 2020/1/17.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SnapKit
import SwiftyUserDefaults
import SwifterSwift
import RxCocoa
import RxSwift

class ST_PairsCenterHeaderView: UIView {

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.cornerRadius = 30
        self.backgroundColor = Color.clear
        
        self.addSubview(bottomContainerView)
        bottomContainerView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.addSubview(bottom_root_view)
        
        
        
        bottom_root_view.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-21)
            make.left.right.equalToSuperview()
            make.height.equalTo(20)
        }
        
        
        self.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-27)
            make.left.equalToSuperview().offset(27)
            make.bottom.equalTo(bottom_root_view.snp.top).offset(-13)
        }
        
        
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
//            make.right.equalToSuperview().offset(-27)
//            make.left.equalToSuperview().offset(27)
            make.centerX.equalToSuperview()
            make.bottom.equalTo(content_label.snp.top).offset(-11)
        }
        
        
        
        
//
        self.addSubview(nobility_image_view)
        nobility_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(18)
            make.centerY.equalTo(titleLabel)
            make.left.equalTo(titleLabel.snp.right).offset(4)
        }
        
        self.addSubview(gift_btn)
        gift_btn.snp.makeConstraints { (make) in
            make.width.height.equalTo(32)
            make.centerY.equalTo(titleLabel)
            make.right.equalToSuperview().offset(-15)
        }
        
        self.addSubview(video_progress_view)
        video_progress_view.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalTo(titleLabel.snp.top).offset(-31)
            make.height.equalTo(ST_PlayVideoProgressView.height)
        }
        
        
        self.addSubview(avatar_image_view)
        avatar_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(60)
            make.bottom.equalTo(gift_btn.snp.top).offset(-45)
            make.right.equalToSuperview().offset(-15)
        }
        
        self.addSubview(icon_supplement_image_view)
        icon_supplement_image_view.snp.makeConstraints { (make) in
            make.center.equalTo(avatar_image_view)
            make.width.height.equalTo(80)
        }
        
        
        self.addSubview(tipsView)
        tipsView.snp.makeConstraints { (make) in
            make.width.equalTo(ST_MyAvatarMatchTipsView.size.width)
            make.height.equalTo(ST_MyAvatarMatchTipsView.size.height)
            make.centerX.equalToSuperview()
            make.bottom.equalTo(video_progress_view.snp.top).offset(-12)
        }
        /* 暂时先取消
        let nextVideoWidth = 150
        self.addSubview(next_video_btn)
        self.addSubview(previous_video_btn)
        next_video_btn.snp.makeConstraints { (make) in
            make.right.top.equalToSuperview()
            make.bottom.equalTo(bottom_root_view.snp.top).offset(-80)
            make.width.equalTo(nextVideoWidth)
        }
        
        previous_video_btn.snp.makeConstraints { (make) in
            make.left.top.equalToSuperview()
            make.bottom.equalTo(bottom_root_view.snp.top).offset(-80)
            make.width.equalTo(nextVideoWidth)
        }
        //        上一个下一个
        let nextVideoWidth = 150
        self.addSubview(next_video_btn)
        self.addSubview(previous_video_btn)
        next_video_btn.snp.makeConstraints { (make) in
            make.right.top.bottom.equalToSuperview()
            make.width.equalTo(nextVideoWidth)
        }
        
        previous_video_btn.snp.makeConstraints { (make) in
            make.left.top.bottom.equalToSuperview()
            make.width.equalTo(nextVideoWidth)
        }
         */
        
        NotificationCenter.default.addObserver(self, selector: #selector(refresh_tips_view), name: Notification.Name.face_status, object: nil)
        
        ST_TIANRouteMonitor.default.didChanged.subscribe(onNext: {[weak self] arg in
        
            if arg.value.to != self?.parentViewController{
                self?.voiceSignControl?.stop_playing()
            }
            
            
        }).disposed(by: rx.disposeBag)
        
        
      }
    
    @objc func refresh_tips_view(){
        
        guard let m = self.model else{
            tipsView.isHidden = true
            return
        }
        
        if m.accid == Defaults[DefaultsKeys.currentUserID] {
            if [2,4].contains(FACE_AVATAE_STATE){
                tipsView.isHidden = false
            }else{
                tipsView.isHidden = true
            }
            tipsView.blue_color_style = FACE_AVATAE_STATE == 4
        
        }else{
                tipsView.isHidden = true
        }
    }
          
      deinit {
          NotificationCenter.default.removeObserver(self)
      }
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    weak var phDelegate:ST_PairsShareParticularViewDelegate?
    
    weak var pairsHeader:ST_PairsShareParticularView?
    
    /**
     和 create_play_view 方法二选一
     */
    func package_play_view(_ view:ST_PairsShareParticularView){
        
        pairsHeader = view
        
        self.insertSubview(view, belowSubview: video_progress_view)
        
        self.insertSubview(faceFailureCoverImageView, belowSubview: video_progress_view)
        
        faceFailureCoverImageView.snp.makeConstraints { (make) in
            make.edges.equalTo(view)
        }
        
        pairsHeader?.progress_closure = { progress,idx in
            self.video_progress_view.setProgress(percentValue: progress, currentIdx: idx)
        }
        
//        view.roundCorners(cornerRadius: 30, allRoundCorner: false)
        
        pairsHeader?.phDelegate = self.phDelegate
        
    }
    //来自外部的 ST_PairsShareParticularView
    fileprivate var outsit_play_view = true
    /**
     和 package_play_view 方法二选一
     */
    func create_play_view(){
        
        let v = ST_PairsShareParticularView()
        
        self.insertSubview(v, belowSubview: video_progress_view)
        
        self.insertSubview(faceFailureCoverImageView, belowSubview: video_progress_view)
        
        faceFailureCoverImageView.snp.makeConstraints { (make) in
            make.edges.equalTo(v)
        }
        
        pairsHeader = v
        
        pairsHeader?.progress_closure = { progress,idx in
            self.video_progress_view.setProgress(percentValue: progress, currentIdx: idx)
        }
        
        pairsHeader?.phDelegate = self.phDelegate
        
        outsit_play_view = false
    }
    
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
        control.numberOfLines = 0
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var avatar_image_view:UIImageView = {
       let control = UIImageView()
        control.contentMode = .scaleAspectFill
        control.cornerRadius = 60/2.0
        return control
    }()
    
    fileprivate lazy var icon_supplement_image_view:UIImageView = {
        let control = UIImageView()
       control.contentMode = UIView.ContentMode.scaleAspectFill
       return control
    }()
    
    fileprivate lazy var vipSIgnImageView:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("首页__会员标示_大"))
        control.isHidden = true
       return control
    }()
    
    
    fileprivate lazy var nobility_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = .scaleAspectFill
       return control
    }()
    
    
    fileprivate lazy var persOfGuideView:PairsGuardSomeOneControl = {
        let control = PairsGuardSomeOneControl()
        control.addTarget(self, action: #selector(centerGuideClick), for: UIControl.Event.touchUpInside)
       return control
    }()
    
    fileprivate lazy var currentGuard:GuardHerSmallControl = {
        let control = GuardHerSmallControl()
        control.addTarget(self, action: #selector(centerGuideClick), for: UIControl.Event.touchUpInside)
       return control
    }()
    
    
    
    fileprivate lazy var gift_btn:UIButton = {
               
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.alpha = 0
        control.setImageForAllStates(ST_ImageUtils.imageName("group_present_gift_icon")!)
        
        control.addTarget(self, action: #selector(present_gift_handle), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = "  "
        control.textColor = Color.init(hexString: "#61666F")
        control.numberOfLines = 0
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    
    
    fileprivate lazy var locationSubLabel:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImageForAllStates(ST_ImageUtils.imageName("全局_icon_位置")!)
        control.setTitleForAllStates("  ")
        control.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: 5, bottom: 0, right: 0)
        control.setTitleColorForAllStates(UIColor.init(hex: 0xACB4C0)!)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.medium)
//        _btn.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;/
        control.titleLabel?.lineBreakMode = .byTruncatingTail
        return control
    }()
    
    
    fileprivate lazy var distance_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#ACB4C0")
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.medium)
        return control
    }()
    
    
    fileprivate lazy var online_sub_label:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImageForAllStates(UIImage.init(named: off_line_image_name)!)
        control.setImage(UIImage.init(named: on_line_image_name), for: UIControl.State.selected)
        control.setTitleForAllStates("  ")
        control.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: 5, bottom: 0, right: 0)
        control.setTitleColorForAllStates(UIColor.init(hex: 0xACB4C0)!)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.medium)
        return control
    }()
    
    
    fileprivate let off_line_image_name = "首页_icon_过往在线"
    fileprivate let on_line_image_name = "首页_icon_现在活跃"
   
    
    
    fileprivate lazy var loveMeSubLabel:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImageForAllStates(ST_ImageUtils.imageName("个人中心_icon_收的喜欢")!)
        control.setTitleForAllStates("  ")
        control.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: 5, bottom: 0, right: 0)
        control.setTitleColorForAllStates(UIColor.init(hex: 0xACB4C0)!)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.medium)
        return control
    }()
    
    fileprivate lazy var faceStatusSubLabel:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImageForAllStates(ST_ImageUtils.imageName("个人中心_icon_真人认证sel")!)
        control.setTitleForAllStates("真人头像认证  ")
        control.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: 5, bottom: 0, right: 0)
        control.setTitleColorForAllStates(UIColor.init(hex: 0xACB4C0)!)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.medium)
        control.addTarget(self, action: #selector(determin_face_status_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    @objc fileprivate func determin_face_status_handle(){
        
        
        if self.model?.accid == Defaults[.currentUserID]{
            return
        }
//        DetectFaceHandle
        switch FACE_AVATAE_STATE {
        case 1:
            DetectFaceHandle()
            break
        case 4:
            let vc = ST_BTHUDController.init(noticeText: "你的头像还在审核中")
            
            GlobalPresent(toController: vc, animated: false)
            
            main_thread_execute(afterDelay: 2.5) {
                vc.dismiss(animated: false, completion: nil)
            }
            break
        case 2:
            let nav = UINavigationController.create_hiding_bar_navigation_controller(rootViewController: ST_MyPhotoViewController())
            
            GlobalPresent(toController: nav)
            break
        default:
            break
        }
    }
    
//    BTVerticalAlertViewController
    fileprivate lazy var bottomContainerView:UIView = {
        let control = UIView()
        control.backgroundColor = Color.white
        return control
    }()
    
    
    fileprivate lazy var bottom_root_view:UIView = {
        let control = UIView()
        control.backgroundColor = Color.clear
        return control
    }()

    fileprivate lazy var video_progress_view:ST_PlayVideoProgressView = {
        let control = ST_PlayVideoProgressView()
        return control
    }()
    
    var voiceSignControl:ST_PairsVoiceSignPlayControl?
    
    fileprivate lazy var tipsView:ST_MyAvatarMatchTipsView = {
        let control = ST_MyAvatarMatchTipsView()
        control.isHidden = true
        return control
    }()
    
    fileprivate lazy var faceFailureCoverImageView:UIImageView =  {
        let control = UIImageView.init(image: UIImage.init(named:
            "个人主页_未认证_icon"))
        control.contentMode = .center
        control.backgroundColor = UIColor.init(white: 0, alpha: 0.3)
        control.isHidden  = true
        return control
    }()
    
    /*
    fileprivate lazy var next_video_btn:UIButton = {
           
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        
//        control.backgroundColor = Color.init(white: 0, alpha: 0.2)
        
        control.addTarget(self, action: #selector(play_next_video), for: UIControl.Event.touchUpInside)
        
        return control
    }()

        
     fileprivate lazy var previous_video_btn:UIButton = {
       
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        
//        control.backgroundColor = Color.init(white: 0, alpha: 0.2)
        
        control.addTarget(self, action: #selector(play_previous_video), for: UIControl.Event.touchUpInside)
        
        return control
    }()*/
    
//    MARK: - Bind Model
    var model:ST_PairsMyCenterHeaderViewModel?
    
    func bind_model(_ model:ST_PairsMyCenterHeaderViewModel){
        
        self.model = model
        
        refresh_tips_view()
        
        titleLabel.text = model.nickname + "  " +  model.age.string + "岁"
        
        refresh_guide_view()
        /*
        vipSIgnImageView.isHidden = CHECK_MEMBER_STATUS(expirationTime: model.vipExpire) != .subscribe
        
        
        */
        if let url = model.nobilityImg?.url{
            
            nobility_image_view.pin_setImage(from: url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
        }else{
            
            nobility_image_view.image = nil
        }
        
        voiceSignControl?.stop_playing()
        voiceSignControl?.removeFromSuperview()
        
        if let url = model.voiceSignUrl,model.voiceSignDuration > 0{
            let voiceSignControl = ST_PairsVoiceSignPlayControl(url: url, duration: model.voiceSignDuration)
            addSubview(voiceSignControl)
            self.voiceSignControl = voiceSignControl
            voiceSignControl.snp.makeConstraints { make in
                make.left.equalToSuperview().offset(13)
                make.bottom.equalTo(video_progress_view.snp.top).offset(-17)
                make.height.equalTo(32)
            }
//            voiceSignControl.play()
        }
        
        
        
        if let first = model.particularArray.first,let url = AVATAR_SUPPLEMENT(guardTime: model.guardTime, guardContent: model.guardContent)?.url {
            
            avatar_image_view.pin_setImage(from: first.imageUrl.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            icon_supplement_image_view.pin_setImage(from: url)
            
            avatar_image_view.isHidden = false
            icon_supplement_image_view.isHidden = false
            
        }else{
            avatar_image_view.isHidden = true
            icon_supplement_image_view.isHidden = true
        }
        
        
        /*
        if model.accid == Defaults[.currentUserID]{
            gift_btn.alpha = 0
        }else{
            gift_btn.alpha = 1
        }*/
        
        
        if let atrr = model.signAttri{
            content_label.attributedText = atrr
        }
        
        self.bottom_root_view.removeSubviews()
        
//        是自己
        if model.accid == Defaults[.currentUserID]{
            
            loveMeSubLabel.setTitleForAllStates(model.liked_num.string+"人喜欢了你"+"  ")
            
            if FACE_AVATAE_STATE == 3{
                
                self.bottom_root_view.addSubview(faceStatusSubLabel)
                faceStatusSubLabel.snp.makeConstraints { (make) in
                    make.left.equalTo(self.bottom_root_view.snp.centerX).offset(10)
                    make.centerY.equalToSuperview()
                }
                
                
                self.bottom_root_view.addSubview(loveMeSubLabel)
                loveMeSubLabel.snp.makeConstraints { (make) in
                    make.right.equalTo(self.bottom_root_view.snp.centerX).offset(-10)
                    make.centerY.equalToSuperview()
                }
                
            }else{
                
                self.bottom_root_view.addSubview(loveMeSubLabel)
                loveMeSubLabel.snp.makeConstraints { (make) in
                    make.center.equalToSuperview()
                }
            }
        }else{
            
            setupLocationDistanceControlValue(model: model)
        }
        

        
        
        
        if outsit_play_view{
            
            set_up_progress_view(fromCount: pairsHeader?.data_array.count ?? 0)
            
        }else{
            
            pairsHeader?.setUp(data_array: model.particularArray)
            
            set_up_progress_view(fromCount: model.particularArray.count)
            
        }
        
        
    }
    
    fileprivate func set_up_progress_view(fromCount:Int){
        
        video_progress_view.create_items(count: fromCount)
        
        video_progress_view.isHidden = fromCount < 2
     
//        print("隐藏video_progress_view :",video_progress_view.isHidden)
        
        main_thread_execute(afterDelay: 0.5) {
            if fromCount > 0{
                self.video_progress_view.setProgress(percentValue: 1, currentIdx: 0)
            }
        }
        
    }
    
    func refresh_guide_view(){
        
        
//        self.model
        
        if let guide = self.model?.guideModel,guide.guardTime.double > Date().timeIntervalSince1970 {
            
            if currentGuard.superview == nil{
                self.addSubview(currentGuard)
                currentGuard.snp.makeConstraints { (make) in
                    make.width.equalTo(100)
                    make.height.equalTo(100)
                    make.right.equalToSuperview().offset(-5)
                    make.bottom.equalTo(titleLabel.snp.top).offset(-41)
                }
            }
            
            currentGuard.bind(title: guide.guardDay.string + "天守护", avatar: guide.guardMemberAvatar)
            
            persOfGuideView.removeFromSuperview()
            
        }else{
            if persOfGuideView.superview == nil{
                self.addSubview(persOfGuideView)
                persOfGuideView.snp.makeConstraints { (make) in
                    make.width.equalTo(76)
                    make.height.equalTo(76 + 5)
                    make.right.equalToSuperview().offset(-17)
                    make.bottom.equalTo(titleLabel.snp.top).offset(-41)
                }
                
            }
            currentGuard.removeFromSuperview()
        }
        
        
        
        
    }
    
    fileprivate func setupLocationDistanceControlValue(model:ST_PairsMyCenterHeaderViewModel){
        
        
            self.bottom_root_view.removeSubviews()
        
            if model.address.count < 1 && model.distance.count < 1{
                return
            }
            
            locationSubLabel.setTitleForAllStates(model.address + " ")
            
            locationSubLabel.isHidden = model.address.count < 1
            
            let distanceStr = model.distance + "  "
            
            distance_label.text = distanceStr
            
            let w = distanceStr.width(withConstrainedHeight: 100, font: UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.medium))
            
            online_sub_label.isSelected = model.online_state == "现在活跃"
            
            online_sub_label.setTitleForAllStates(model.online_state + "  ")
            
            let distanceOffset = 3.cgFloat
            
            let maxLocationWidth = 80+63-w
            
            var btnWidth = model.address.width(withConstrainedHeight: 100, font: UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.medium)) + 5 + 2 + 15
            
            
            print("当前可以的 显示地址的Start Width:",btnWidth)
            
            if btnWidth > maxLocationWidth{
                btnWidth = maxLocationWidth
            }
            
            
            print("当前可以的 显示地址的Width:",btnWidth)
            
            
            if model.face_audit_state == 3{
                
                faceFailureCoverImageView.isHidden = true
                
                if model.is_show_active {
                    
                    self.bottom_root_view.addSubview(online_sub_label)
                    online_sub_label.snp.remakeConstraints { (make) in
                        make.center.equalToSuperview()
                    }
                    
                    self.bottom_root_view.addSubview(distance_label)
                    distance_label.snp.remakeConstraints { (make) in
                        make.centerY.equalToSuperview()
                        make.right.equalTo(self.online_sub_label.snp.left).offset(-15)
                    }
                    
                    self.bottom_root_view.addSubview(locationSubLabel)
                    locationSubLabel.snp.remakeConstraints { (make) in
                        make.centerY.equalToSuperview()
                        make.right.equalTo(self.distance_label.snp.left).offset(-distanceOffset)
                        make.width.equalTo(btnWidth)
                    }
                    
                    self.bottom_root_view.addSubview(faceStatusSubLabel)
                    faceStatusSubLabel.snp.remakeConstraints { (make) in
                        make.left.equalTo(self.online_sub_label.snp.right).offset(15)
                        make.centerY.equalToSuperview()
                    }
                    
                    
                }else{
                     
                    self.bottom_root_view.addSubview(distance_label)
                    distance_label.snp.makeConstraints { (make) in
                        make.centerY.equalToSuperview()
                        make.right.equalTo(self.bottom_root_view.snp.centerX).offset(-10)
                    }
                    
                    self.bottom_root_view.addSubview(locationSubLabel)
                    locationSubLabel.snp.makeConstraints { (make) in
                        make.centerY.equalToSuperview()
                        make.right.equalTo(self.distance_label.snp.left).offset(-distanceOffset)
                        make.width.equalTo(btnWidth)
                    }
                    
                    self.bottom_root_view.addSubview(faceStatusSubLabel)
                    faceStatusSubLabel.snp.makeConstraints { (make) in
                        make.left.equalTo(self.bottom_root_view.snp.centerX).offset(10)
                        make.centerY.equalToSuperview()
                    }
                }
            }else{
                
                if model.is_show_active {
                    
                    self.bottom_root_view.addSubview(online_sub_label)
                    online_sub_label.snp.makeConstraints { (make) in
                        make.left.equalTo(self.bottom_root_view.snp.centerX).offset(10)
                        make.centerY.equalToSuperview()
                    }
                    
                    self.bottom_root_view.addSubview(distance_label)
                    distance_label.snp.makeConstraints { (make) in
                        make.centerY.equalToSuperview()
                        make.right.equalTo(self.bottom_root_view.snp.centerX).offset(-10)
                    }
                    
                    self.bottom_root_view.addSubview(locationSubLabel)
                    locationSubLabel.snp.makeConstraints { (make) in
                        make.centerY.equalToSuperview()
                        make.right.equalTo(self.distance_label.snp.left).offset(-distanceOffset)
                        make.width.equalTo(btnWidth)
                    }
                    
                }else{
                    
                    self.bottom_root_view.addSubview(locationSubLabel)
                    self.bottom_root_view.addSubview(distance_label)
                    
                    distance_label.snp.makeConstraints { (make) in
                        make.centerY.equalToSuperview()
                        make.left.equalTo(self.locationSubLabel.snp.right).offset(-distanceOffset)
                    }
                    
                    
                    locationSubLabel.snp.makeConstraints { (make) in
                        make.centerY.equalToSuperview()
                        make.centerX.equalTo(self.bottom_root_view).offset(-20)
                        make.width.equalTo(btnWidth)
                    }
                    
                    
                }
                
            }
        
        
        
        
    }
    
}

extension ST_PairsCenterHeaderView{
    
    @objc fileprivate func play_next_video(){
        
        self.pairsHeader?.scrollToNext()
    }
    
    @objc fileprivate func play_previous_video(){
        self.pairsHeader?.scrollToPrevious()
    }
    
    @objc fileprivate func centerGuideClick(){
    
        guard let m = self.model,let herGuard = self.model?.guideModel , m.accid != Defaults[.currentUserID] else { return  }
        
        let ta = m.sex == .female ? "她" : "他"
        
        if m.isMyGuard{
            
            let vc  = GuardHerMaxAlertController()
            
            vc.dataModel = herGuard
            
            vc.overCurrentContext = true
            
            vc.info = (title:"你是\(ta)的唯一甜蜜守护",content:"除非有人取代了你，在有效期内你\n都是\(ta)的唯一甜蜜守护人哦！")
            
            GlobalPresent(toController: vc)
            
            return
        }
        
        
        var alert:UIViewController!
        
//        最大守护
        if herGuard.isMaxGuard && herGuard.guardTime.double > Date().timeIntervalSince1970{
            let vc  = GuardHerMaxAlertController()
            
            vc.dataModel = herGuard
            
            vc.info = (title:"\(ta)已经收到最高等级守护",content:"最高等级的守护不可被取代，等守护\n到期了之后再来守护\(ta)吧！")
            
            alert = vc
        }else{
//            let vc  = ST_GuardHerAlertController()
            let vc  = ST_GuardHerNowAlertViewController()

            vc.dataModel = herGuard
            
            vc.ta = ta
            
            vc.sendGiftClosure = {[weak self] in
                self?.pushPresentGuideHandel()
            }
            alert = vc
        }
        
        alert.overCurrentContext = true
        
        GlobalPresent(toController: alert, animated: false)
    }
    
    
    fileprivate func pushPresentGuideHandel(){
        
        
        guard let m = self.model,let g = m.guideModel else{
            return
        }
        
        let giftAlert = ST_GiftOfGuideController()
        
        giftAlert.overCurrentContext = true
        
        giftAlert.over_current_context_alpha = "0.4"
        
        giftAlert.userId = m.accid
        
        giftAlert.avatar = g.herAvatar
        
        giftAlert.present_goods_closure = {[weak self] g in
            
            let model = ST_DTChatMessage()
            
            model.chatID = self?.model?.accid ?? ""
            
            model.messageTime = Int64(Date().timeIntervalSince1970)
            
            model.cellStyle = .incomeGuard
            
            model.direction = .send
            
            guard let account = ST_BTUserInfoManager.shared.current_account_info,let userInfo = account.userinfo else { return }
            
            model.portraitUri = userInfo.avatar
            
            model.userName = userInfo.nickname
            
            model.goods = g
            
//            model.uploadStatus = .success
            
            model.figureFrame()
            
            ST_DTChatMessageSendHander.shared.sendMessage(message: model)
            
            
        }
        
        GlobalPresent(toController: giftAlert)
        
    }
    
    @objc fileprivate func present_gift_handle(){
      
    }
}




//MARK: -
class PairsGuardSomeOneControl:UIControl {
    
//    height  76 + 5
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(animateView)
        animateView.snp.makeConstraints { (make) in
            make.width.height.equalTo(76)
            make.top.centerX.equalToSuperview()
        }
        
        self.addSubview(gradient_view)
        gradient_view.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview()
            make.width.equalTo(56)
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
        }
        
        self.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalTo(gradient_view)
        }
        
        animateView.play()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    fileprivate lazy var animateView:LOTAnimationView = {
        
        let path = Bundle.main.path(forResource: "pairsGuard", ofType: "json")!
        
        let control = LOTAnimationView.init(filePath: path)
        
        control.contentMode = UIView.ContentMode.scaleAspectFill
        
        control.isUserInteractionEnabled = false
        
        control.loopAnimation = true
        
        return control
    }()
    
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = "甜蜜守护"
        control.textColor = UIColor.white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 11, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var gradient_view:ST_FWGradientView = {
        let control = ST_FWGradientView()
        control.cornerRadius = 10
        control.bgLayer.colors = [UIColor.init(hex: 0xF1D6A0)!.cgColor, UIColor.init(hex: 0xEDC26A)!.cgColor]
        control.bgLayer.locations = [0, 1]
        control.bgLayer.startPoint = CGPoint(x: 0, y: 0)
//        [UIColor(red: 0.12, green: 0.12, blue: 0.12, alpha: 0).cgColor, UIColor(red: 0.02, green: 0.02, blue: 0.02, alpha: 0.2).cgColor, UIColor(red: 0, green: 0, blue: 0, alpha: 0.4).cgColor]
        
        control.bgLayer.endPoint = CGPoint(x: 0, y: 1)
        return control
    }()
    
}
